<?php defined('BASEPATH') OR exit('No direct script access allowed');

class PushLog extends Admin_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('orderCar_m');
        $this->load->model('order_m');
        $this->load->model('carService_m');
        $this->load->model('carInfo_m');
        $this->load->model('orderAudit_m');
        $this->load->model('carSeat_m');
        $this->load->model('orderRober_m');
        $this->load->model('areas_m');
        $this->load->model('orderType_m');
        $this->load->model('pushLog_m');
    }

    public function index()
    {
        $get               = $this->input->get();
        $page              = $get['per_page'] ?? 1;
        $current_url_query = $this->current_url_query($get);

        //分页
        $limit                       = 20;
        $config['uri_segment']       = 4;
        $config['use_page_numbers']  = true;
        $config['page_query_string'] = true;
        $config['total_rows']        = $this->pushLog_m->count_logs($get['order_status'] ?? "", "", $get);
        $config['base_url']          = base_url('manage/OrderCar/index?') . $current_url_query;
        $config['per_page']          = $limit;
        $config['num_links']         = 1;
        $start                       = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $logs               = $this->pushLog_m->get_log_list($start, $limit, $get);

        if (!empty($logs)) {
            foreach ($logs as $key => $val) {
                $logs[$key]['order_type_name'] = $this->carService_m->get_car_service_name($val['order_service_id']);

                if (!empty($val['push_users'])) {
                    $users      = [];
                    $push_users = json_decode($val['push_users']);
                    if($push_users){
                        foreach ($push_users as $k => $v) {

                            $user = $this->user_m->findBy(["user_id" => $v], "user_id,user_name");

                            $users[$k]['user_name'] = $user['user_name'];
                            $car_info           = $this->carInfo_m->findBy(['user_id' => $v]);
                            if ($car_info) {
                                $car_seat          = $this->carSeat_m->find($car_info['car_seat']);
                                $users[$k]['car_seat'] = $this->carSeat_m->get_car_level()[$car_seat['car_level']] . $car_seat['car_seat'] . "座";
                            }else{
                                $users[$k]['car_seat'] = "无";
                            }
                        }
                        $logs[$key]['push_users'] = $users;
                    }else{
                        $logs[$key]['push_users'] = [];
                    }

                }

                if ($val['car_requirement']) {
                    $car_seat = $this->carSeat_m->find($val['car_requirement']);
                    if ($car_seat) {
                        $car_level                     = $this->carSeat_m->get_car_level()[$car_seat['car_level']];
                        $logs[$key]['car_seat_format'] = $car_level . $car_seat['car_seat'] . "座";
                    } else {
                        $logs[$key]['car_seat_format'] = "<span style='color: red'>未选择车型要求</span>";
                    }
                }else{
                    $logs[$key]['car_seat_format'] = "<span style='color: red'>未选择车型要求</span>";
                }
            }
        }

        $data['logs']         = $logs;
        $this->load->view('pushlog/index', $data);
    }

    private function current_url_query($get)
    {
        $queryData = [];
        if (isset($get['order_status'])) {
            $queryData['order_status'] = $get['order_status'] ?? "";
        }
        if (isset($get['order_no'])) {
            $queryData['order_no'] = $get['order_no'];
        }
        if (isset($get['starttime'])) {
            $queryData['starttime'] = $get['starttime'];
        }
        if (isset($get['endtime'])) {
            $queryData['endtime'] = $get['endtime'];
        }
        return http_build_query($queryData);
    }

}
